package org.csu.lestore.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.csu.lestore.api.common.CommonResponse;
import org.csu.lestore.api.entity.*;
import org.csu.lestore.api.persistence.AccountMapper;
import org.csu.lestore.api.persistence.BrowseMapper;
import org.csu.lestore.api.persistence.FavMapper;
import org.csu.lestore.api.persistence.ImgMapper;
import org.csu.lestore.api.service.AccountService;
import org.csu.lestore.api.util.ImgUtil;
import org.csu.lestore.api.vo.AccountVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("accountService")
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private FavMapper favMapper;
    @Autowired
    private ImgMapper imgMapper;

    @Override
    public CommonResponse<AccountVO> getAccountByUserName(String userName, String password) {
        QueryWrapper<Account> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_name",userName);
        queryWrapper.eq("password",password);

        Account account= accountMapper.selectOne(queryWrapper);
        if(account==null){
            return CommonResponse.createForError("用户名或密码错误");
        }

        return getAccountByUserName(userName);
    }

    @Override
    public CommonResponse<AccountVO> getAccountByUserName(String userName) {
        QueryWrapper<Account> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_name",userName);

        Account account= accountMapper.selectOne(queryWrapper);
        if(account==null){
            return CommonResponse.createForError("根据用户名获取用户信息失败");
        }

        AccountVO accountVO=accountToAccountVO(account);
        return CommonResponse.createForSuccess(accountVO);
    }

    @Override
    public CommonResponse<AccountVO> getAccountByUserId(int userId, String password) {
        QueryWrapper<Account> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("password",password);

        Account account= accountMapper.selectOne(queryWrapper);
        if(account==null){
            return CommonResponse.createForError("账号或密码错误");
        }
        return getAccountByUserId(userId);
    }

    @Override
    public CommonResponse<AccountVO> getAccountByUserId(int userId) {
        QueryWrapper<Account> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);

        Account account= accountMapper.selectOne(queryWrapper);
        if(account==null){
            return CommonResponse.createForError("根据账号获取用户信息失败");
        }

        AccountVO accountVO=accountToAccountVO(account);
        return CommonResponse.createForSuccess(accountVO);
    }

    @Override
    public CommonResponse<AccountVO> getAccountByUserTelephone(String telephoneNumber, String password) {
        QueryWrapper<Account> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("telephone_number",telephoneNumber);
        queryWrapper.eq("password",password);

        Account account= accountMapper.selectOne(queryWrapper);
        if(account==null){
            return CommonResponse.createForError("电话或密码错误");
        }
        return getAccountByUserTelephone(telephoneNumber);
    }

    @Override
    public CommonResponse<AccountVO> getAccountByUserTelephone(String telephoneNumber) {
        QueryWrapper<Account> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("telephone_number",telephoneNumber);

        Account account= accountMapper.selectOne(queryWrapper);
        if(account==null){
            return CommonResponse.createForError("根据电话获取用户信息失败");
        }

        AccountVO accountVO=accountToAccountVO(account);
        return CommonResponse.createForSuccess(accountVO);
    }

    @Override
    public CommonResponse<Account> updateAccount(Account account) {
        QueryWrapper<Account> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",account.getUserId());

        accountMapper.update(account,queryWrapper);

        return CommonResponse.createForSuccessMessage("修改成功");
    }

    @Override
    public CommonResponse<AccountVO> updateAccountImg(int userId, String imgSrc) {
        QueryWrapper<Account> accountQueryWrapper = new QueryWrapper<>();
        accountQueryWrapper.eq("user_id",userId);
        Account account = accountMapper.selectOne(accountQueryWrapper);

        Img img = new Img();
        img.setImgSrc(imgSrc);
        //有图片
        if(account.getImgId()!=-1){
            img.setImgId(account.getImgId());
            QueryWrapper<Img> imgQueryWrapper = new QueryWrapper<>();
            imgQueryWrapper.eq("img_id",img.getImgId());
            imgMapper.update(img,imgQueryWrapper);
        }else{
            imgMapper.insert(img);
            account.setImgId(img.getImgId());
            updateAccount(account);
        }

        return CommonResponse.createForSuccess(accountToAccountVO(account));
    }

    @Override
    public CommonResponse<Account> insertAccount(AccountVO accountVO) {
        AccountVO existAccountVO1 = getAccountByUserTelephone(accountVO.getTelephoneNumber()).getData();
        AccountVO existAcccountVO2 = getAccountByUserName(accountVO.getUserName()).getData();
        if(existAccountVO1!=null||existAcccountVO2!=null){
            String msg = "";
            if(existAccountVO1!=null){
                msg+="电话号已存在";
            }
            if(existAcccountVO2!=null){
                msg+="用户名已存在";
            }
            return CommonResponse.createForError(msg);
        }

        Account account = new Account();
        account.setUserId(accountVO.getUserId());
        account.setUserName(accountVO.getUserName());
        account.setPassword(accountVO.getPassword());
        account.setSex(accountVO.getSex());
        account.setTelephoneNumber(accountVO.getTelephoneNumber());
        account.setRegisterDate(accountVO.getRegisterDate());
        account.setStatus(accountVO.getStatus());

        if(accountVO.getImgSrc()!=null){
            Img img = new Img();
            img.setImgSrc(accountVO.getImgSrc());
            imgMapper.insert(img);
            account.setImgId(img.getImgId());
        }else{
            account.setImgId(-1);
        }

        accountMapper.insert(account);
        return CommonResponse.createForSuccess("新增用户成功",account);
    }
/*

*/
    @Override
    public CommonResponse<Account> deleteAccount(int userId) {
        QueryWrapper<Fav> favQueryWrapper = new QueryWrapper<>();
        favQueryWrapper.eq("user_id",userId);

        QueryWrapper<Account> accountQueryWrapper = new QueryWrapper<>();
        accountQueryWrapper.eq("user_id",userId);

        int imgId = accountMapper.selectOne(accountQueryWrapper).getImgId();
        QueryWrapper<Img> imgQueryWrapper = new QueryWrapper<>();
        imgQueryWrapper.eq("img_id",imgId);

        imgMapper.delete(imgQueryWrapper);
        favMapper.delete(favQueryWrapper);
        accountMapper.delete(accountQueryWrapper);

        return CommonResponse.createForSuccessMessage("删除用户成功");
    }

    @Override
    public CommonResponse<AccountVO> userNameIsExist(String userName) {
        QueryWrapper<Account> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_name",userName);

        Account account= accountMapper.selectOne(queryWrapper);
        if(account != null){
            return getAccountByUserName(userName);
        }
        return CommonResponse.createForError("用户名不存在");
    }

    @Override
    public CommonResponse<AccountVO> telephoneIsExist(String telephoneNumber) {
        QueryWrapper<Account> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("telephone_number",telephoneNumber);

        Account account= accountMapper.selectOne(queryWrapper);
        if(account != null){
            return getAccountByUserTelephone(telephoneNumber);
        }
        return CommonResponse.createForError("电话不存在");
    }

    @Override
    public CommonResponse<IPage<AccountVO>> getAccountListInPage(int curPage, int size) {
        IPage<Account> accountPage = new Page<>(curPage,size);
        accountMapper.selectPage(accountPage,null);

        List<Account> accountList = accountPage.getRecords();
        List<AccountVO> accountVOList = new ArrayList<>();
        IPage<AccountVO> accountVOPage = new Page<>(curPage,size);

        if(accountList != null) {
            for (Account account : accountList) {
                AccountVO accountVO = accountToAccountVO(account);
                accountVOList.add(accountVO);
            }
            accountVOPage.setTotal(accountPage.getTotal());
            accountVOPage.setPages(accountPage.getPages());
            accountVOPage.setRecords(accountVOList);
            return CommonResponse.createForSuccess(accountVOPage);
        }
        return CommonResponse.createForError("获取账号列表失败");
    }

    @Override
    public CommonResponse<IPage<AccountVO>> getAccountListInPage(int curPage, int size, char status) {
        IPage<Account> accountPage = new Page<>(curPage,size);
        QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",status);
        accountMapper.selectPage(accountPage,queryWrapper);

        List<Account> accountList = accountPage.getRecords();
        List<AccountVO> accountVOList = new ArrayList<>();
        IPage<AccountVO> accountVOPage = new Page<>(curPage,size);

        if(accountList != null) {
            for (Account account : accountList) {
                AccountVO accountVO = accountToAccountVO(account);
                accountVOList.add(accountVO);
            }
            accountVOPage.setTotal(accountPage.getTotal());
            accountVOPage.setPages(accountPage.getPages());
            accountVOPage.setRecords(accountVOList);
            return CommonResponse.createForSuccess(accountVOPage);
        }
        return CommonResponse.createForError("根据账号类型获取账号列表失败");
    }

    @Override
    public CommonResponse<IPage<AccountVO>> getAccountListInPageF(int curPage, int size, char status) {
        IPage<Account> accountPage = new Page<>(curPage,size);
        QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("status",status);
        accountMapper.selectPage(accountPage,queryWrapper);

        List<Account> accountList = accountPage.getRecords();
        List<AccountVO> accountVOList = new ArrayList<>();
        IPage<AccountVO> accountVOPage = new Page<>(curPage,size);

        if(accountList != null) {
            for (Account account : accountList) {
                AccountVO accountVO = accountToAccountVO(account);
                accountVOList.add(accountVO);
            }
            accountVOPage.setTotal(accountPage.getTotal());
            accountVOPage.setPages(accountPage.getPages());
            accountVOPage.setRecords(accountVOList);
            return CommonResponse.createForSuccess(accountVOPage);
        }
        return CommonResponse.createForError("根据账号类型获取账号列表失败");
    }

    @Override
    public AccountVO accountToAccountVO(Account account){
        AccountVO accountVO = new AccountVO();

        accountVO.setUserId(account.getUserId());
        accountVO.setUserName(account.getUserName());
        accountVO.setPassword(account.getPassword());
        accountVO.setSex(account.getSex());
        accountVO.setTelephoneNumber(account.getTelephoneNumber());
        accountVO.setRegisterDate(account.getRegisterDate());
        accountVO.setStatus(account.getStatus());
        accountVO.setImgId(account.getImgId());

        QueryWrapper<Img> imgQueryWrapper=new QueryWrapper<>();
        imgQueryWrapper.eq("img_id",account.getImgId());
        Img img = imgMapper.selectOne(imgQueryWrapper);
        if(img!=null){
            accountVO.setImgSrc(ImgUtil.getImageBaseSrc(img.getImgSrc(),"user"));
        }else{
            accountVO.setImgId(-1);
        }

        QueryWrapper<Fav> favQueryWrapper = new QueryWrapper<>();
        favQueryWrapper.eq("user_id",account.getUserId());
        List<Fav> favList = favMapper.selectList(favQueryWrapper);
        accountVO.setFavList(favList);

        return accountVO;
    }
}
